#include "Engine.h"
#include "IKeyListener.h"
#include <windows.h>
#include "IContext.h"
#undef main
#include "Vectors.h"
#include "FreeCamera.h"
#include "WalkingCamera.h"
#include "HeightMapTerrain.h"
#include "BaseDefs.h"
#include "Model3DTerrain.h"
#include "IBox.h"
#include "IPhysicsSimulator.h"
#include "IContext.h"
#include "World.h"
#include "..\..\..\Boiler\codebase\include\EntityUpdater.h"

bool gameOver = false;

class Terminator : public steam::intake::IKeyListener
{
public:

	void keyPressed( const steam::intake::KeyEvent* evt ) 
	{
		if (evt->getKeyCode() == steam::intake::SK_ESCAPE)
		{
			gameOver = true;
			std::cout << "********************* Quiting *********************\n";
		}
	}

	void keyReleased( const steam::intake::KeyEvent* evt ) 
	{
	}

};


int main()
{
	steam::core::Engine& engine = steam::core::Engine::create();
	
	engine.getInputHandler().addKeyListener(new Terminator);

	engine.getRenderer().getSkyBox().initialize("..\\..\\Resources\\skybox1\\", "PNG");

	engine.getRenderer().getSkyBox().setVisible(true);
	steam::coal::IMesh* cubeMesh = engine.getAssetLoader().loadModelFromFile("concrete\\concrete.obj", "OBJ");
	engine.getRenderer().createAndRegisterMesh(cubeMesh);

	steam::coal::IMesh* truckMesh = engine.getAssetLoader().loadModelFromFile("truck\\Truck.obj", "OBJ");
	engine.getRenderer().createAndRegisterMesh(truckMesh);

	steam::coal::IMesh* terrainMesh = engine.getAssetLoader().loadModelFromFile("floor\\floor.obj", "OBJ");
	steam::moon::ITerrain* terrain = new steam::moon::Model3DTerrain(terrainMesh, steam::base::Scale(5));
	engine.getRenderer().createAndRegisterMesh(terrain->getMesh());



	//steam::moon::ITerrain* terrain = new steam::moon::HeightMapTerrain("..\\..\\Resources\\terrain\\heightmap.jpg", "..\\..\\Resources\\terrain\\texture.jpg", steam::base::DEFAULT_SCALE);
	//engine.getRenderer().setTerrain(terrain);
	//engine.getRenderer().createAndRegisterMesh(terrain->getMesh());
	
	steam::base::Vector3 pos(0,30,20);
	//steam::moon::WalkingCamera* camera = new steam::moon::WalkingCamera(pos, 0, 0);
	steam::moon::ICamera* camera = new steam::moon::FreeCamera(pos, 0, 0);
	//camera->setTerrain(terrain);

	engine.getRenderer().setCamera(camera);
	engine.getInputHandler().addKeyListener(camera);

	steam::wheel::BoxInfo info;
	info.Size = steam::base::Vector3(2, 2, 2);
	info.Position = steam::base::Vector3(0, 100, -20);
	info.Mass = 2.f;

	steam::wheel::IBox* box = engine.getPhysicsSimulator().createBox(info, steam::wheel::SIMULATE_AUTO);
	engine.getRenderer().registerBox(box);
	steam::firebox::getContext().getWorld().addUpdater(new steam::boiler::EntityUpdater());
	//engine.getRenderer().registerEntity((steam::firebox::getContext().getWorld()).m_entity);
	while (engine.run() && !gameOver)
	{
		float elapsedTime = engine.getTimer().getElapsedTimeInSec();
		//std::cout << engine.getTimer().getElapsedTimeInSec() << std::endl;
		engine.getPhysicsSimulator().advance(elapsedTime);
		engine.getRenderer().render();
		steam::firebox::getContext().getWorld().update(elapsedTime);
		//Sleep(100);
		//std::cout << terrain->getHeightAt(camera->getPosition().x, camera->getPosition().z) << "\n";		
	}

	engine.destroy();
	delete camera;

	Sleep(100);

	return 0;
}